package com.ruijie.autotest.log.common.crypto;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;

public final class CryptoFactory implements ICryptoFactory {

	private final CipherInitializer initializer;

	public CryptoFactory(CipherInitializer initializer) {
		this.initializer = initializer;
	}

	public byte[] doCipher(int cryptoMode, byte[] rawBytes) {
		try {
			return initializer.createCipher(cryptoMode).doFinal(rawBytes);
		} catch (IllegalBlockSizeException | BadPaddingException e) {
			throw new RuntimeException("doCipher failed.", e);
		}
	}

	@Override
	public Crypto encryption() {
		return rawBytes -> doCipher(Cipher.ENCRYPT_MODE, rawBytes);
	}

	@Override
	public Crypto decryption() {
		return rawBytes -> doCipher(Cipher.DECRYPT_MODE, rawBytes);
	}
}
